/**
 * 
 */
package ee.fujitsu.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Random;
import java.util.StringTokenizer;

import ee.fujitsu.Main;
import ee.fujitsu.entity.Customer;
import ee.fujitsu.entity.JournalRecord;
import ee.fujitsu.entity.Movie;
import ee.fujitsu.entity.state.Store;
import ee.fujitsu.exception.ServiceException;

/**
 * The Class TestInitImpl.
 *
 * @author Andrei
 * 
 * 09.09.2012
 */
public class TestInitImpl implements Init{

	  /** The Constant CUSTOMER_FILE_NAME. */
  	final static String CUSTOMER_FILE_NAME = "/customers.csv";
	  
  	/** The Constant MOVIE_FILE_NAME. */
  	final static String MOVIE_FILE_NAME = "/movies.csv";
	  
	  /** The Constant ENCODING. */
  	final static Charset ENCODING = StandardCharsets.UTF_8;
	  
	  /** The Constant IO_EXCEPTION. */
  	final static int IO_EXCEPTION = 12;
	  
  	/** The Constant FILE_NOT_FOUND. */
  	final static int FILE_NOT_FOUND = 15;
	  
  	/** The Constant NUMBER_FORMAT. */
  	final static int NUMBER_FORMAT = 13;
	  
  	/** The Constant NO_SUCH_ELEMENT. */
  	final static int NO_SUCH_ELEMENT = 14;
	  
	  /**
  	 * Generates entity for initial journal
  	 * 
  	 * Use {@link #generateJournal( customers, movies)} for generate entity.
  	 *
  	 * @param customers the customers
  	 * @param store the store
  	 * @return           DOM document
  	 * @throws ServiceException the service exception
  	 */
	public Map<Integer, JournalRecord> getJournal(Map <Integer, Customer> customers,
			Store store) throws ServiceException {
		
	    Map <Integer, JournalRecord> records = new HashMap<Integer, JournalRecord>();
	    int k=0;
	    
	     for (int j = 0; j < 10; j++) {
	 	    List<Integer> customerIndexes = new ArrayList<Integer>(customers.keySet());
				int filmQty = 1 + new Random().nextInt(25);
			    int cind = customerIndexes.get(new Random().nextInt(customerIndexes.size()-1));
				Customer client = customers.get(cind);
				Date[] dates = Utils.getDates(2, new Random().nextInt(4));
				Date startDate = dates[0]; 
				Date expectedEnd = dates[1];
				Date realEnd = dates[2];
			      
			    for (int i = 0; i < filmQty; i++) {
			    	  JournalRecord record = store.rentFilm(startDate, expectedEnd, realEnd, client, false);
			    	  records.put(k++, record);
				}
	     }		

		return records;
	}

	/**
	 * Gets the movies.
	 *
	 * @return the movies
	 * @throws ServiceException the service exception
	 */
	@SuppressWarnings("unused")
	public Map<Integer, Movie> getMovies() throws ServiceException {
		String line = null;
		Map<Integer, Movie> movies = new HashMap<Integer, Movie>();

		BufferedReader bufRdr = null;
		try {

			bufRdr = new BufferedReader(new InputStreamReader(
					Main.class.getResourceAsStream(MOVIE_FILE_NAME), ENCODING));
			int id = 0;
			String title = null;
			String type = null;
			String desc = null;
			String released = null;

			while ((line = bufRdr.readLine()) != null) {
				StringTokenizer st = new StringTokenizer(line, ",");
				Movie movie = null;
				while (st.hasMoreTokens()) {
					id = Integer.parseInt(st.nextToken());
					title = st.nextToken();
					desc = st.nextToken();
					type = st.nextToken();
					released = st.nextToken();
				}
				movie = new Movie(title, type);
				movies.put(id, movie);
			}
			bufRdr.close();
		} catch (NoSuchElementException e) {
			throw new ServiceException(NO_SUCH_ELEMENT, e.getMessage());
		} catch (NumberFormatException e) {
			throw new ServiceException(NUMBER_FORMAT, e.getMessage());
		} catch (IOException e) {
			throw new ServiceException(FILE_NOT_FOUND, e.getMessage());
		}
		return movies;
	}

	/**
	 * Gets the customers.
	 *
	 * @return the customers
	 * @throws ServiceException the service exception
	 */
	public Map<Integer, Customer> getCustomers()
			throws ServiceException {
		String line = null;
		Map<Integer, Customer> customers = new HashMap<Integer, Customer>();

		BufferedReader bufRdr = null;
		try {

			bufRdr = new BufferedReader(new InputStreamReader(
					ClassLoader.class.getResourceAsStream(CUSTOMER_FILE_NAME),
					ENCODING));
			int id = 0;
			String name = null;
			String desc = null;

			while ((line = bufRdr.readLine()) != null) {
				StringTokenizer st = new StringTokenizer(line, ",");
				Customer customer = null;
				while (st.hasMoreTokens()) {
					id = Integer.parseInt(st.nextToken());
					name = st.nextToken();
					desc = st.nextToken();
				}
				customer = new Customer(name, desc);
				customers.put(id, customer);
			}
			bufRdr.close();
		} catch (NumberFormatException e) {
			throw new ServiceException(NUMBER_FORMAT, e.getMessage());
		} catch (IOException e) {
			throw new ServiceException(FILE_NOT_FOUND, e.getMessage());
		}

		return customers;
	}	

}
